#include <sonar/srf05.h>

#include <arduino/Arduino.h>


namespace sonar
{


void srf05::loop(void)
{
  pinMode(_pin, OUTPUT);

  // Make sure pin is low before sending a short high to trigger ranging
  digitalWrite(_pin, LOW);

  delayMicroseconds(2);

  // Send a short 10 microsecond high burst on pin to start ranging
  digitalWrite(_pin, HIGH);

  delayMicroseconds(10);

  // Send pin low again before waiting for pulse back in
  digitalWrite(_pin, LOW);

  pinMode(_pin, INPUT);

  // Reads echo pulse in from SRF05 in micro seconds
  _duration = pulseIn(_pin, HIGH);

  // Dividing this by 58 gives us a distance in cm
  _measure = _duration/58;
}


} // sonar
